//
//  ForthController.m
//  WalkiOS
//
//  Created by luqiuan on 2019/10/12.
//  Copyright © 2019 Chenmuzhou. All rights reserved.
//

#import "ForthController.h"
#import "WAMineCell.h"
#import "WAIdentityAuthController.h"
#import "WAAddressController.h"
#import "WATransactionRecordController.h"
#import "WASettingController.h"
#import "WAMyTeamController.h"
#import "WAMemberCenterController.h"
#import "WAMallCenterController.h"
#import "WAServicesController.h"
#import "WAInviteFriendController.h"

@interface ForthController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *authIdentityLB;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headImageViewTopCons;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *headImageViewHeightCons;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nickNameLB;
@property (weak, nonatomic) IBOutlet UILabel *userIDLB;
@property (weak, nonatomic) IBOutlet UILabel *userLeveLB;
@property (weak, nonatomic) IBOutlet UILabel *activeLeveLB;
@property (weak, nonatomic) IBOutlet UILabel *yuluLB;

@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *backViewTopCons;
@property (weak, nonatomic) IBOutlet UIView *topBackView;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *layout;


@property (copy, nonatomic) NSArray *dataSources;
@end

@implementation ForthController{
    CGFloat scroll_offset_Y;
    CGFloat headDefaultImageViewTopCons;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSources = @[@{@"name":@"会员中心",@"image":@"huiyuan"},
                          @{@"name":@"我的交易",@"image":@"jiaoyi"},
                          @{@"name":@"商城订单",@"image":@"dingdan"},
                          @{@"name":@"收货地址",@"image":@"dizhi"},
                          @{@"name":@"邀请好友",@"image":@"haoyou"},
                          @{@"name":@"我的团队",@"image":@"tuandui"}
    ];
    
    CGFloat padding = 15;
    _layout.minimumLineSpacing = padding;//上册cell的距离
    _layout.sectionInset = UIEdgeInsetsMake(padding, padding, 0, padding);
    CGFloat item_W = (NSInteger)((WNScreenWidth - padding * 4 - 4)/3);
    CGFloat item_H = item_W;
    _layout.itemSize = CGSizeMake( item_W, item_H);
    _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    [self.collectionView registerNib:[UINib nibWithNibName:@"WAMineCell" bundle:nil] forCellWithReuseIdentifier:@"WAMineCell"];
    
    [self.topView setNeedsLayout];
    [self.topView layoutIfNeeded];
    [self.collectionView setContentInset:UIEdgeInsetsMake(self.topView.bottom, 0, 0, 0)];
    self.backViewTopCons.constant = self.topView.bottom;
    scroll_offset_Y = self.topView.bottom;
    [self.headImageView setNeedsLayout];
    [self.headImageView layoutIfNeeded];
    headDefaultImageViewTopCons = self.headImageViewTopCons.constant;
    [self updateViews];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (![self.loginManager isloginOnline]) {
        [self.loginManager presentLoginView:nil];
        return;
    }
    [self.loginManager updateUserHeadImageAndUserInfo:^(BOOL loginSuccess) {
        [self updateViews];
    }];
}

- (void)updateViews{
    WNUserInfo *info = self.loginManager.userInfo;
    self.nickNameLB.text = [info phone];
    if (info.nickname.length) {
        self.nickNameLB.text = info.nickname;
    }
    self.userIDLB.text = [NSString stringWithFormat:@"用户ID:%zd",info.userid];
    self.userLeveLB.text = [NSString stringWithFormat:@"LV %@",info.viplevel];
    self.activeLeveLB.text = [NSString stringWithFormat:@"%@",info.activitylevel];
    self.yuluLB.text = [NSString stringWithFormat:@"%.1f",info.candynumber.floatValue];
    if ([info.headimage hasPrefix:@"http"]) {
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:info.headimage] placeholderImage:[UIImage imageNamed:@"head_default_photo"]];
    }else if (info.headimage.length > 0) {
        self.headImageView.image = [UIImage stringToImage:info.headimage];
    }else{
        self.headImageView.image = [UIImage imageNamed:@"head_default_photo"];
    }
    
    if ([self.loginManager isAuthentication]) {
        self.authIdentityLB.text = @"实名认证";
        self.authIdentityLB.backgroundColor = [UIColor colorWithHexString:@"FEE400"];
        self.authIdentityLB.textColor = [UIColor whiteColor];
    }else{
        self.authIdentityLB.text = @"未认证";
        self.authIdentityLB.backgroundColor = [UIColor grayColor];
        self.authIdentityLB.textColor = [UIColor blackColor];
    }
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    [self.topView cornerTopLeftAndTopRight:7 byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
}

#pragma mark - UICollectionView Delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSources.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WAMineCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WAMineCell" forIndexPath:indexPath];
    NSDictionary *dict = self.dataSources[indexPath.row];
    cell.nameLB.text = dict[@"name"];
    cell.headImageView.image = [UIImage imageNamed:dict[@"image"]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.dataSources[indexPath.row];
    NSString *name = dict[@"name"];
    
    if ([name isEqualToString:@"我的交易"]) {
        WATransactionRecordController *VC = [WATransactionRecordController new];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
    }else if ([name isEqualToString:@"收货地址"]){
        WAAddressController *VC = [WAAddressController new];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
    }else if ([name isEqualToString:@"实名认证"]){
        WAIdentityAuthController *VC = [WAIdentityAuthController new];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
    }else if ([name isEqualToString:@"我的团队"]){
        WAMyTeamController *VC = [WAMyTeamController new];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
    }else if ([name isEqualToString:@"会员中心"]){
        WAMemberCenterController *VC = [WAMemberCenterController new];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
    }else if ([name isEqualToString:@"商城订单"]){
        WAMallCenterController *VC = [WAMallCenterController new];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
    }else if ([name isEqualToString:@"邀请好友"]){
//        [WNTool show:@"功能正在完善中"];
        WAInviteFriendController *VC = [WAInviteFriendController new];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.backViewTopCons.constant = -scrollView.contentOffset.y;
    CGFloat fab_V = 44 - (scrollView.contentOffset.y + scroll_offset_Y);

    if (fab_V >= 44) {
        fab_V = 44;
    }
    if (fab_V < - self.headImageViewHeightCons.constant - 15) {
        fab_V = - self.headImageViewHeightCons.constant - 15;
    }
    self.headImageViewTopCons.constant = fab_V;
}

- (IBAction)settingAction:(id)sender {
    WASettingController *VC = [WASettingController new];
    VC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:VC animated:YES];
}

//头像
- (IBAction)headImageAction:(id)sender {
    WAIdentityAuthController *VC = [WAIdentityAuthController new];
    VC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:VC animated:YES];
}

//客服
- (IBAction)servicesAction:(id)sender {
    WAServicesController *VC = [WAServicesController new];
    VC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:VC animated:YES];
}
@end
